#include<iostream>
using namespace std;
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int n,m;
    cin>>n>>m;
    int ap[n],c[m];
    for(int i=0;i<n;i++) cin>>ap[i];
    for(int i=0;i<m;i++) cin>>c[i];
    int all=1,K=0;
    while(all<n){all*=2;K++;}
    string d[K];
    for(int R=0;R<K;R++) cin>>d[R];
    int T;
    cin>>T;
    for(int t=0;t<T;t++)
    {
        int X[4];
        cin>>X[0]>>X[1]>>X[2]>>X[3];
        int a[n];
        for(int i=0;i<n;i++) a[i]=ap[i]^X[i%4];
        int A[m];
        for(int i=0;i<m;i++)
        {
            if(c[i]==1){A[i]=1;continue;}
            int lose[all]{0};
            for(int R=0;R<K;R++)
            {
                for(int G=0;G<K-R;G++)
                {
                    if(c[i]%2==0)
                    {
                        int first=0,second=0;
                        for(int j=0;j<all;j++){if(lose[j]==0){first=j;break;}}
                        for(int j=first+1;j<all;j++){if(lose[j]==0){second=j;break;}}
                        if(d[R][G]=='0')
                        {
                            A[i]+=a[first]>=R?first:second;
                            lose[a[first]>=R?second:first]=1;
                        }
                        else
                        {
                            A[i]+=a[second]>=R?second:first;
                            lose[a[second]>=R?first:second]=1;
                        }
                    }
                }
            }
        }
        int ans=0;
        for(int i=0;i<m;i++) ans+=i^A[i];
        cout<<ans<<endl;
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
